package com.itheima.web.controller;

import cn.hutool.core.util.StrUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.MovementVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.web.manager.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserManager userManager;

    /**
     * 用户列表数据
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/manage/users")
    public PageBeanVo findUsers( @RequestParam(defaultValue = "1" , name = "page") Integer pageNumber ,
            @RequestParam(defaultValue = "10", name = "pagesize" )Integer pageSize) {
        return userManager.findUsers(pageNumber, pageSize);
    }

    /**
     * 查询用户详情
     */
    @GetMapping("/manage/users/{userId}")
    public UserInfo findUserById(@PathVariable("userId") Long userId) {
        return userManager.findUserById(userId);
    }

    /**
     * 分页查询视频列表
     */
    @GetMapping("/manage/videos")
    public PageBeanVo findVideoByUserId( @RequestParam(defaultValue = "1" , name = "page") Integer pageNumber ,
            @RequestParam(defaultValue = "10", name = "pagesize" )Integer pageSize,
            Long uid) {
        return userManager.findVideoByUserId(pageNumber,pageSize,uid);
    }


    /**
     * 动态列表
     */
    @GetMapping("/manage/messages")
    public PageBeanVo findMovementList( @RequestParam(defaultValue = "1" , name = "page") Integer pageNumber ,
            @RequestParam(defaultValue = "10", name = "pagesize" )Integer pageSize,
            Long uid, String state) {
        Integer stateInt = null;
        //前端有坑  没有state的时候  传入了"" 空串    需要判断    这是前端的小bug
        if (StrUtil.isNotBlank(state)) {
            stateInt = Integer.valueOf(state);//转换成int
        }
        return userManager.findMovementList(pageNumber, pageSize, uid, stateInt);
    }


    /**
     * 查询动态详情
     */
    @GetMapping("/manage/messages/{movementId}")
    public MovementVo findMovementById( @PathVariable String movementId) {

        return userManager.findMovementById(movementId);
    }


    /**
     * 查询评论列表
     */
    @GetMapping("/manage/messages/comments")
    public PageBeanVo findCommentList(   @RequestParam(defaultValue = "1" , name = "page") Integer pageNumber ,
            @RequestParam(defaultValue = "10", name = "pagesize" )Integer pageSize,
            @RequestParam(name = "messageID") String movementId) {

        return userManager.findCommentList(pageNumber, pageSize, movementId);
    }


    /**
     * 动态审核是否通过
     */
    @PostMapping({"/manage/messages/pass","/manage/messages/reject"})
    public Map<String, String> auditMovementIfPass(@RequestBody List<String> movementIds) {
        //System.out.println("我被调用了");
        //System.out.println(movementIds);
        //设置操作类型
        Boolean option = true;
        Boolean flag = userManager.auditMovementIfPass(movementIds,option);
        Map<String, String> resultMap = new HashMap<>();
        if (flag) {
            resultMap.put("message", "审批通过");
        } else {
            resultMap.put("message", "审批不通过");
        }
        return resultMap;

    }



}
